﻿namespace UnityEditor
{
    using System;

    public class VCMenuChange
    {
        [UnityEditor.MenuItem("CONTEXT/Change/Delete Empty Changelist", false, 0x12e)]
        private static void DeleteChangelist(MenuCommand cmd)
        {
            VCProvider.DeleteChanges(VCListControl.FromID(cmd.userData).SelectedChanges);
            VCDatabase.Invalidate();
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Delete Empty Changelist", true)]
        private static bool DeleteChangelistTest(MenuCommand cmd)
        {
            return VCProvider.DeleteChangeIsValid(VCListControl.FromID(cmd.userData).SelectedChanges);
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Edit Changelist...", false, 0x12d)]
        private static void EditChangelist(MenuCommand cmd)
        {
            VCChangelist changeList = GetChangeList(VCListControl.FromID(cmd.userData).SelectedChanges);
            if (changeList != null)
            {
                VCWindowChange.Open(changeList, new VCAssetList(), false);
            }
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Edit Changelist...", true)]
        private static bool EditChangelistTest(MenuCommand cmd)
        {
            VCChanges selectedChanges = VCListControl.FromID(cmd.userData).SelectedChanges;
            return ((selectedChanges.Count > 0) && VCProvider.SubmitIsValid(selectedChanges[0], null));
        }

        private static VCChangelist GetChangeList(VCChanges changes)
        {
            if (changes.Count == 0)
            {
                return null;
            }
            return changes[0];
        }

        [UnityEditor.MenuItem("CONTEXT/Change/New Changelist...", false, 300)]
        private static void NewChangelist(MenuCommand cmd)
        {
            VCWindowChange.Open(new VCAssetList(), false);
        }

        [UnityEditor.MenuItem("CONTEXT/Change/New Changelist...", true)]
        private static bool NewChangelistTest(MenuCommand cmd)
        {
            return VCProvider.isActive;
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Revert...", false, 200)]
        private static void Revert(MenuCommand cmd)
        {
            VCChangelist changeList = GetChangeList(VCListControl.FromID(cmd.userData).SelectedChanges);
            if (changeList != null)
            {
                VCWindowRevert.Open(changeList);
            }
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Revert...", true)]
        private static bool RevertTest(MenuCommand cmd)
        {
            return (VCListControl.FromID(cmd.userData).SelectedChanges.Count > 0);
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Revert Unchanged", false, 0xc9)]
        private static void RevertUnchanged(MenuCommand cmd)
        {
            VCProvider.RevertChanges(VCListControl.FromID(cmd.userData).SelectedChanges, true);
            VCDatabase.Invalidate();
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Revert Unchanged", true)]
        private static bool RevertUnchangedTest(MenuCommand cmd)
        {
            return (VCListControl.FromID(cmd.userData).SelectedChanges.Count > 0);
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Submit...", false, 100)]
        private static void Submit(MenuCommand cmd)
        {
            VCChangelist changeList = GetChangeList(VCListControl.FromID(cmd.userData).SelectedChanges);
            if (changeList != null)
            {
                VCWindowChange.Open(changeList, new VCAssetList(), true);
            }
        }

        [UnityEditor.MenuItem("CONTEXT/Change/Submit...", true)]
        private static bool SubmitTest(MenuCommand cmd)
        {
            VCChanges selectedChanges = VCListControl.FromID(cmd.userData).SelectedChanges;
            return ((selectedChanges.Count > 0) && VCProvider.SubmitIsValid(selectedChanges[0], null));
        }
    }
}

